using CommonDomain;
using Raven.Client;
using Sponsorship.Domain;
using Sponsorship.Infrastructure.BootStrapping.Registries;
using StructureMap;

namespace Sponsorship.Infrastructure.BootStrapping
{
    /// <summary>
    /// Bootstraps the application by invoking all registries found within this assembly in order to configure StructureMap with command handlers,
    /// repositories, event store, and bus. After that, calls <see cref="EventRoutesRegistrar"/>.
    /// </summary>
    public class BootStrapper
    {
        public static readonly string RavenDbConnectionStringName = "RavenDB";

        public static IContainer BootStrap(IDocumentStore store)
        {
            ObjectFactory.Initialize(x =>
            {
                x.For<IDocumentStore>().Use(store);
                x.For<IRouteEvents>().Use<RelaxedConventionEventRouter>();

   
                //have you started RavenDB?
                //-> packages\RavenDB.1.0.499\server\Raven.Server.exe

                x.Scan(scan =>
                           { 
                               scan.LookForRegistries();
                               scan.Assembly(typeof(CommandHandlerRegistry).Assembly);
                           });
            });

            var bus = ObjectFactory.GetInstance<IBus>();
            var eventHandlerRegistrar = new EventRoutesRegistrar(bus);
            eventHandlerRegistrar.RegisterRoutesForAllEvents();

            return ObjectFactory.Container;
        }
    }
}